package com.tm.dao.impl;

import com.tm.dao.IStudentDao;
import com.tm.entity.Student;
import com.tm.util.DBUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements IStudentDao {
    
    private Connection conn = null;
    private Statement stmt = null;
    private ResultSet rs = null;

    @Override
    public List<Student> getAllStudents() {

        return null;
    }

    @Override
    public List<Student> getStudentsByPage(int cp, int ps) {

        List<Student> list = null;

        conn = DBUtil.getConn();

        int si = (cp - 1) * ps;

        try {
            String sql = "select * from stu limit " + si + ", " + ps;

            stmt = conn.createStatement();

            rs = stmt.executeQuery(sql);

            if(rs != null){
                list = new ArrayList<>();

                Student stu = null;
                while (rs.next()){
                    stu = new Student();

                    stu.setStuId(rs.getInt(1));
                    stu.setStuName(rs.getString(2));
                    stu.setStuSex(rs.getString(3));
                    stu.setClId(rs.getInt(4));

                    list.add(stu);
                }
            }

            DBUtil.closeAll(conn, stmt, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public Student getStudentBySid(int sid) {
        Student stu = null;

        conn = DBUtil.getConn();

        String sql = "select * from stu where s_id = " + sid;

        try {
            stmt = conn.createStatement();

            rs = stmt.executeQuery(sql);

            if (rs.next()){
                stu = new Student();

                stu.setStuId(rs.getInt(1));
                stu.setStuName(rs.getString(2));
                stu.setStuSex(rs.getString(3));
                stu.setClId(rs.getInt(4));

            }

            DBUtil.closeAll(conn, stmt, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return stu;
    }

    @Override
    public int saveStudent(Student s) {
        int result = 0;
        conn = DBUtil.getConn();
        String sql = "insert into stu values(null, '" + s.getStuName() + "', '" + s.getStuSex() + "', " + s.getClId() + ")";
        try {
            stmt = conn.createStatement();

            result = stmt.executeUpdate(sql);

            DBUtil.closeAll(conn, stmt, rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }
    @Override
    public int updateStudent(Student s) {
        int result = 0;

        conn = DBUtil.getConn();

        String sql = "update student set s_name = '" + s.getStuName() + "', s_sex = '" + s.getStuSex() + "', c_id = " + s.getClId() + " where s_id = " + s.getStuId();

        try {
            stmt = conn.createStatement();

            result = stmt.executeUpdate(sql);

            DBUtil.closeAll(conn, stmt, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return result;
    }

    @Override
    public int deleteStudent(Student s) {
        return 0;
    }

    @Override
    public int deleteStudent(int sid) {
        int result = 0;
        try {
            conn = DBUtil.getConn();
            stmt = conn.createStatement();
            String sql = "delete from stu where s_id = " + sid;
            result = stmt.executeUpdate(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
